/**
 * @author qiuxj
 * @text 责任片区管理
 */
var area = {
    init: function(){
        // 定义DWR
        var dwr = {
            createArea: areaDwr.createArea,
            updateArea: areaDwr.updateArea,
            getArea: areaDwr.getArea,
            getCompanyKls: companyDwr.getCompanyKls,
            pageArea: areaDwr.pageArea
        };
        
        //定义分公司下拉列表
        var companyCbx = new CQCIS.form.DWRComboBox({
            ds: {
                call: dwr.getCompanyKls
            },
            width: 120,
            emptyText: '请选择局站',
            resizable: true,
            listeners: {
                'select': function(o){
                    grid.filter.companyId = o.getValue();
                    grid.reload();
                }
            }
        });
        
        // 增加按钮
        var addBtn = new CQCIS.Button({
            text: '添加 (<u>A</u>)',
            iconCls: 'btn_insert',
            handler: function(){
            		areaWin.load();
            		areaWin.form.findField("companyId").setValue(companyCbx.getValue());
            }
        });
        
        // 修改按钮
        var editBtn = new CQCIS.Button({
            text: '修改 (<u>E</u>)',
            iconCls: 'btn_modify',
            handler: function(){
                if (grid.getSelectedRow()) {
                    areaWin.load(grid.getSelectedRow()['areaId']);
                } else {
                    CQCIS.msg("请选择要修改的片区信息！");
                }
            }
        });
        
        // 添加信息表单字段
        var areaFormMeta = [{
            id: 'areaId',
            desc: '片区ID',
            field: 'areaId',
            type: 'int',
            xtype: 'hidden'
        }, {
            field: 'areaName',
            desc: '片区名称'
        }, {
            field: 'areaDesc',
            desc: '片区描述'
        }, {
            field: 'companyId',
            desc: '分公司',
            type: 'int',
            xtype: 'rcombo',
            obj: {
                ds: {
                    call: dwr.getCompanyKls,
                    fields: ['key', 'label']
                },
                emptyText: '请选择分公司',
                allowBlank: false
            },
            width: 80
        }, {
            field: 'note',
            desc: '备注',
            xtype: 'htmleditor'
        }, {
            field: 'staffId',
            desc: '创建人ID',
            type: 'int',
            xtype: 'hidden'
        }, {
            field: 'createTime',
            desc: '创建时间',
            type: 'date',
            xtype: 'hidden'
        }, {
            field: 'modifyTime',
            desc: '修改时间',
            type: 'date',
            xtype: 'hidden'
        }, {
            field: 'state',
            desc: '状态',
            type: 'int',
            xtype: 'acombo',
            obj: {
                ds: {
                    data: [[0, '失效'], [1, '有效']],
                    fields: ['key', 'label']
                },
                value: 1
            }
        }];
        
        // 列表数据对象
        var areaPageMeta = [{
            id: 'areaId',
            desc: '片区ID',
            field: 'areaId',
            type: 'int',
            hidden: true
        }, {
            desc: '创建人ID',
            field: 'staffId',
            type: 'int',
            hidden: true
        }, {
            desc: '片区名称',
            field: 'areaName'
        }, {
            desc: '片区描述',
            field: 'areaDesc'
        }, {
            desc: '分公司ID',
            field: 'companyId',
            type: 'int',
            hidden: true
        }, {
        	desc: '分公司名称',
            field: 'companyName'
        }, {
            field: 'note',
            desc: '备注'
        }, {
            field: 'state',
            desc: '状态',
            renderer: function(val){
                var str;
                switch (val) {
                	case '0':
                		str = '失效';
                    	break;
                    case '1':
                        str = '有效';
                        break;
                    default:
                        str = '';
                        break;
                }
                return str;
            }
        }, {
            field: 'createTime',
            desc: '创建时间',
            type: 'time'
        }, {
            field: 'modifyTime',
            desc: '修改时间',
            type: 'time'
        }];
        
         // 定义表单
        var areaForm = new CQCIS.form.DWRForm({
            dwr: {
                load: dwr.getArea,
                save: dwr.createArea,
                update: dwr.updateArea
            },
            metas: areaFormMeta,
            listeners: {
                'saveafter': function(){
                    CQCIS.msg('片区信息保存成功！');
                    grid.reload();
                }
            }
        });
        
        // 定义窗口
        var areaWin = new CQCIS.form.WinForm({
            title: '添加片区信息',
            width: 660,
            height: 380,
            items: areaForm
        });
        
        // 查看片区详细信息
        var areaDetailWin = new CQCIS.form.WinForm({
            title: '详细信息',
            width: 450,
            height: 280,
            autoScroll: true,
            html: '',
            buttons: [{
                text: '关闭',
                handler: function(){
                    areaDetailWin.hide();
                }
            }],
            listeners: {
            	'beforeshow': function(){
            		if(!grid.getSelectedRow()){
            			CQCIS.msg('请选择您要查看的片区！');
            			return false;
            		}
            	},
                'show': function(o){
                    if (o.rendered) {
                    	var filter = {
                    		areaId: grid.getSelectedRow().areaId
                    	};
                    	var like = {};
                    	var sort = {};
						dwr.pageArea(filter, like, sort, 1, 1, {
							callback: function(lv){
								var htmlStr = [];
								var obj = lv.result[0];
								
								htmlStr.push('<table border="1" cellpadding="0" cellspacing="1" width="100%" >');
								htmlStr.push('<tr><td width="100">片区名称:</td><td>',obj.areaName,'</td></tr>');
								htmlStr.push('<tr><td width="100">所属分公司:</td><td>',obj.companyName,'</td></tr>');
								htmlStr.push('<tr><td width="100">片区描述:</td><td>',obj.areaDesc,'</td></tr>');
								htmlStr.push('<tr><td width="100">备注信息:</td><td>',obj.note,'</td></tr>');
								htmlStr.push('</table>');
								var str = {
									inner: htmlStr.join('')
								};
								o.body.dom.innerHTML = htmlStr.join('');
							}
						});
                    } else {
                        o.show.defer(100, o);
                    }
                }
            }
        });
        
        var detailBtn = new CQCIS.Button({
            text: '详情 (<u>L</u>)',
            iconCls: 'btn_insert',
			handler: function(){ 
			 	areaDetailWin.load(); 
            }
        });
        
        // 片区信息grid
        var grid = new CQCIS.grid.PageGrid({
            firstColumn: 'radio',
            dwr: {
                call: dwr.pageArea
            },
            filter: {},
            sortable: true,
            metas: areaPageMeta,
            tbar: ['-','请选择局站：', '-', companyCbx, '-', addBtn, '-', editBtn, '-', detailBtn],
            bbar: true,
            initload: true,
            listeners: {
            	'render' : function() {
	            		if(top.CURRENTUSER.loginlevel > 2){
	            			editBtn.hide();
	            			addBtn.hide();
	            		}
				},
                'rowdblclick': editBtn.handler,
                'rowcontextmenu': function(o, rowIndex, e){
                    e.stopEvent();
                    //定义增加右键菜单
                    var addMenu = new Ext.menu.Item({
                        iconCls: 'menu_insert',
                        id: 'addMenu',
                        text: '增加',
                        handler: addBtn.handler
                    });
                    //定义修改右键菜单
                    var editMenu = new Ext.menu.Item({
                        iconCls: 'menu_modify',
                        id: 'editMenu',
                        text: '修改',
                        handler: editBtn.handler
                    });
                    //定义列表右键菜单
                    var gridMenu = new Ext.menu.Menu({
                        id: 'gridMenu',
                        items: [addMenu, editMenu]
                    });
                    //gridMenu.showAt(e.getXY());
                }
            }
        });
        grid.render();
    }
};

Ext.onReady(area.init, area);
